home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Main.bin
/
JMaskedTextField.java
< prev
next >
Wrap
Text File
|
1998-10-19
|
11KB
|
324 lines
package com.symantec.itools.swing;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusAdapter;
import java.awt.datatransfer.*;
import com.sun.java.swing.text.Document;
import com.sun.java.swing.text.PlainDocument;
import com.sun.java.swing.event.DocumentListener;
import com.sun.java.swing.event.DocumentEvent;
public class JMaskedTextField extends com.sun.java.swing.JTextField {
/* These constants are used to specify the data type information needed
to specialize input time behavior based on data type.
For now, the only such difference is between text and numeric:
In numeric fields, an empty field is initialzed to zero, and input of
a decimal point when the mask contains one and the cartet is to its left
causes the caret to move to the right of the decimal point position.
*/
public static final int texttype = MaskEngine.texttype;
public static final int numbtype = MaskEngine.numbtype;
public static final int datetype = MaskEngine.datetype;
public static final int timetype = MaskEngine.timetype;
// ctors
public JMaskedTextField ( ) { this("" , 0 ); }
public JMaskedTextField (int numberOfColumns) { this("" , numberOfColumns); }
public JMaskedTextField (String initialText ) { this(initialText, 256 ); }
public JMaskedTextField (String initialText, int numberOfColumns) {
this(null, initialText, numberOfColumns);
}
// All other constructors call this one
public JMaskedTextField(Document doc, String initialText, int numberOfColumns) {
super(doc, initialText, numberOfColumns);
}
/**
Initialize the field
*/
public synchronized void setMaskedText(String t) {
if(getMask().equals("")){
setText(t);
}else{
StringBuffer newData = new StringBuffer();
_firstInputPos = _maskEngine.initDisplay(t, newData);
setText(newData.toString());
if (_haveFocus && isEditable())
select(_firstInputPos, _firstInputPos + 1);
}
}
// Returns current text with mask characters removed
public synchronized String getUnmaskedText() {
if(getMask().equals(""))
return getText();
StringBuffer newData = new StringBuffer();
_dataComplete = _maskEngine.stripMask(getText(), newData);
return newData.toString();
}
/**
* This is a standard AWT method which gets called when
* this component is added to a container.
*
* @see #removeNotify
*/
public synchronized void addNotify() {
if (_focusListener == null) {
_focusListener = new FocusAdapter() {
public void focusGained(FocusEvent e) { gotFocus (e); }
public void focusLost (FocusEvent e) { lostFocus(e); }
};
addFocusListener(_focusListener);
}
super.addNotify();
}
/**
* This method gets called when this component is removed from a
* container.
*
* @see #addNotify
*/
public synchronized void removeNotify() {
if (_focusListener != null) {
removeFocusListener(_focusListener);
_focusListener = null;
}
super.removeNotify();
}
public synchronized void cut(Clipboard clipboard) {
if (!isEditable())
return;
_activity = true;
StringBuffer newData = new StringBuffer();
int selStart = getSelectionStart();
String clipboardData = _maskEngine.cut(getText(), selStart,
getSelectionEnd(),
newData);
StringSelection ss = new StringSelection(clipboardData);
clipboard.setContents(ss, ss);
setText(newData.toString());
setCaretPosition(selStart);
}
public synchronized void paste(Clipboard clipboard) {
if (!isEditable())
return;
_activity = true;
StringBuffer newData = new StringBuffer();
String data = "";
try {
data = (String)clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {}
int pos = getCaretPosition();
int selStart = getSelectionStart();
int selEnd = getSelectionEnd();
int newpos = _maskEngine.paste(getText(), data, pos, newData,
selStart, selEnd);
if (newpos < 0) // beep if paste failed
getToolkit().beep();
if (newpos == -2) // quit if filter mismatch
return;
setText(newData.toString());
if (newpos >= 0) { // if good new caret position
select(newpos, newpos + 1); // select this position
} else {
select(0, 0); // turn off selection
if (newpos != -1) // cursor just moved out of range
setCaretPosition(newpos + 1000); // move one past last filter
}
}
// Override of JTextComponent
public void cut() {
//vasu start
if(getMask().equals("")){
super.cut();
}else{
//vasu end
cut(java.awt.Toolkit.getDefaultToolkit().getSystemClipboard());
}
}
// Override of JTextComponent
public void paste() {
//vasu start
if(getMask().equals("")) {
super.paste();
}else{
//vasu end
paste(java.awt.Toolkit.getDefaultToolkit().getSystemClipboard());
}
}
public void setMask (String mask) { _maskEngine.setMask (mask); }
public String getMask ( ) { return _maskEngine.getMask ( ); }
public void setDatatype(int type) { _maskEngine.setDatatype(type); }
public int getDatatype( ) { return _maskEngine.getDatatype( ); }
public boolean isDataComplete() {
//vasu start
if(getMask().equals("")) return true;
//vasu end
if (!_dataComplete) {
if (!_activity)
return true;
_dataComplete = _maskEngine.stripMask(getText(), new StringBuffer());
}
return _dataComplete;
}
protected void gotFocus(FocusEvent e) {
_activity = false;
_haveFocus = true;
if (getText().length() == 0)
setMaskedText(""); // show the mask literals if field is empty
else if (isEditable() && !getMask().equals(""))//vasu
select(_firstInputPos, _firstInputPos + 1);
}
protected void lostFocus(FocusEvent e) {
//select(0, 0);//vasu
_haveFocus = false;
if (isEditable() && _activity) {
try {
_docListenerDisabled = true;
_document.remove(0, _document.getLength());
_document.insertString(0, getUnmaskedText(), null);
} catch (com.sun.java.swing.text.BadLocationException ex) {
badLocException();
} finally {
_docListenerDisabled = false;
}
}
}
// Override JTextComponent
protected void processComponentKeyEvent(KeyEvent e) {
//vasu start - because we check whether or not something
// was done before we update the model
_activity = true;
//vasu end
switch(e.getID()) {
case KeyEvent.KEY_TYPED:
if (_maskEngine.isHandledKey(e) && isEditable())
processKey(e);
else
super.processComponentKeyEvent(e);
break;
case KeyEvent.KEY_PRESSED:
if (_maskEngine.isHandledKey(e) && isEditable()) {
e.consume();
if (_keyPressed) { // key must be auto-repeating
if (Character.isISOControl(e.getKeyChar()))
processKey(e);
} else
_keyPressed = true;
} else
super.processComponentKeyEvent(e);
break;
case KeyEvent.KEY_RELEASED:
_keyPressed = false;
if (_maskEngine.isHandledKey(e) && isEditable()) {
if (Character.isISOControl(e.getKeyChar()))
processKey(e);
} else
super.processComponentKeyEvent(e);
break;
}
}
protected void processKey(KeyEvent e) {
_activity = true;
e.consume();
int pos = getSelectionStart();
StringBuffer newData = new StringBuffer("");
String data = getText();
int newpos = _maskEngine.processKey(e, pos, data, newData,
pos, getSelectionEnd());
if (newpos == -2) // quit if filter mismatch
return;
setText(newData.toString());
if (newpos >= 0) { // if good new caret position
select(newpos, newpos + 1); // select this position
} else if (newpos == -1) {
getToolkit().beep();
select(0, 0);
} else { // cursor just moved out of range
select(0, 0); // turn off selection
setCaretPosition(newpos + 1000); // move one past last filter
}
}
// Override
// This gets called during superclass construction!
public void setDocument(Document d) {
if (_docListener == null)
_docListener = new DocumentListener() {
public void changedUpdate(DocumentEvent e) { docChange(e); }
public void removeUpdate (DocumentEvent e) { docRemove(e); }
public void insertUpdate (DocumentEvent e) { docInsert(e); }
};
if (_document != null)
_document.removeDocumentListener(_docListener);
_document = d;
_document.addDocumentListener(_docListener);
if (_myDoc == null)
_myDoc = new PlainDocument();
super.setDocument(_myDoc);
}
// These shouldn't get called
protected void docChange(DocumentEvent e) {
if (_docListenerDisabled)
return;
// System.out.println("Masked Doc Listener Change Called");
}
protected void docRemove(DocumentEvent e) {
if (_docListenerDisabled)
return;
// System.out.println("Masked Remove Called");
setText("");
}
// This gets called on data insertion into the field
protected void docInsert(DocumentEvent e) {
if (_docListenerDisabled)
return;
int len = e.getLength();
String data = "";
try {
data = _document.getText(0, len);
} catch(com.sun.java.swing.text.BadLocationException ex) {
badLocException();
}
setMaskedText(data);
}
protected void badLocException() {
getToolkit().beep();
System.out.println("Bad Location Exception in JMaskedTextField");
}
private PlainDocument _myDoc = null;
private DocumentListener _docListener = null;
private Document _document = null;
private boolean _docListenerDisabled = false;
private boolean _dataComplete = true; // true iff no mandatory filters are empty
private boolean _haveFocus = false; // true if this component has input focus
private boolean _keyPressed = false; // true if a key is down
private boolean _activity = false;
private int _firstInputPos = 0; // position of first filter
private MaskEngine _maskEngine = new MaskEngine();
private FocusAdapter _focusListener = null;
}